load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
load("//bazel:canisters.bzl", "rust_canister")
load("//bazel:defs.bzl", "rust_ic_test_suite_with_extra_srcs")
load("//bazel:prost.bzl", "generated_files_check")

package(default_visibility = ["//visibility:public"])

filegroup(
    name = "protos",
    srcs = glob(["proto/**/*.proto"]),
)

# See rs/nervous_system/feature_test.md
DEPENDENCIES = [
    # Keep sorted.
    "//packages/ic-http-types",
    "//packages/icrc-ledger-types:icrc_ledger_types",
    "//rs/crypto/sha2",
    "//rs/nervous_system/clients",
    "//rs/nervous_system/common",
    "//rs/nervous_system/proto",
    "//rs/nervous_system/runtime",
    "//rs/nns/constants",
    "//rs/nns/handlers/root/interface",
    "//rs/sns/governance",
    "//rs/sns/init",
    "//rs/sns/root",
    "//rs/types/base_types",
    "//rs/types/management_canister_types",
    "//rs/types/types",
    "//rs/utils",
    "@crate_index//:candid",
    "@crate_index//:futures",
    "@crate_index//:hex",
    "@crate_index//:ic-cdk",
    "@crate_index//:ic-metrics-encoder",
    "@crate_index//:ic-wasm",
    "@crate_index//:maplit",
    "@crate_index//:prost",
    "@crate_index//:serde",
    "@crate_index//:serde_bytes",
    "@crate_index//:serde_json",
]

MACRO_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:async-trait",
]

DEV_DEPENDENCIES = [
    # Keep sorted.
    "//rs/ledger_suite/icp:icp_ledger",
    "//rs/ledger_suite/icrc1",
    "//rs/ledger_suite/icrc1/ledger",
    "//rs/nervous_system/common/test_utils",
    "//rs/nns/sns-wasm/protobuf_generator:lib",
    "//rs/nns/test_utils",
    "//rs/registry/helpers",
    "//rs/rust_canisters/canister_test",
    "//rs/sns/swap",  # FIXME
    "//rs/state_machine_tests",
    "//rs/test_utilities",
    "//rs/test_utilities/compare_dirs",
    "//rs/test_utilities/types",
    "@crate_index//:candid_parser",
    "@crate_index//:pretty_assertions",
    "@crate_index//:tempfile",
    "@crate_index//:tokio",
] + select({
    "@rules_rust//rust/platform:wasm32-unknown-unknown": [],
    "//conditions:default": [
        "//rs/registry/canister",
        "//rs/registry/keys",
        "//rs/interfaces/registry",
    ],
})

MACRO_DEV_DEPENDENCIES = []

ALIASES = {}

rust_library(
    name = "sns-wasm",
    srcs = glob([
        "src/**",
    ]),
    aliases = ALIASES,
    crate_name = "ic_sns_wasm",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "1.0.0",
    deps = DEPENDENCIES,
)

rust_canister(
    name = "sns-wasm-canister",
    srcs = ["canister/canister.rs"],
    aliases = ALIASES,
    compile_data = ["canister/sns-wasm.did"],
    proc_macro_deps = MACRO_DEPENDENCIES,
    service_file = ":canister/sns-wasm.did",
    deps = DEPENDENCIES + [":sns-wasm"],
)

rust_test(
    name = "canister_unit_test",
    srcs = glob(["canister/**/*.rs"]),
    compile_data = ["canister/sns-wasm.did"],
    crate_root = "canister/canister.rs",
    proc_macro_deps = MACRO_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES + [":sns-wasm"],
)

rust_test(
    name = "sns-wasm_test",
    aliases = ALIASES,
    crate = ":sns-wasm",
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    deps = DEPENDENCIES + DEV_DEPENDENCIES,
)

rust_ic_test_suite_with_extra_srcs(
    name = "sns-wasm_integration_test",
    srcs = glob(
        ["tests/**/*.rs"],
        exclude = [
            "tests/common/mod.rs",
            "tests/check_generated_files.rs",
        ],
    ),
    aliases = ALIASES,
    data = [
        # Keep sorted.
        ":sns-wasm-canister",
        "//rs/ledger_suite/icp/ledger:ledger-canister-wasm",
        "//rs/ledger_suite/icrc1/archive:archive_canister",
        "//rs/ledger_suite/icrc1/index-ng:index_ng_canister",
        "//rs/ledger_suite/icrc1/ledger:ledger_canister",
        "//rs/nns/cmc:cycles-minting-canister",
        "//rs/nns/governance:governance-canister-test",
        "//rs/nns/gtc:genesis-token-canister",
        "//rs/nns/handlers/lifeline/impl:lifeline_canister",
        "//rs/nns/handlers/root/impl:root-canister",
        "//rs/nns/integration_tests:unstoppable-sns-root-canister",
        "//rs/node_rewards/canister:node-rewards-canister",
        "//rs/registry/canister:registry-canister",
        "//rs/registry/canister:registry-canister-test",
        "//rs/sns/governance:sns-governance-canister",
        "//rs/sns/root:sns-root-canister",
        "//rs/sns/swap:sns-swap-canister",
        "//rs/universal_canister/impl:universal_canister.wasm.gz",
        "@mainnet_canisters//:sns-wasm.wasm.gz",
    ],
    env = {
        "CARGO_MANIFEST_DIR": "rs/nns/sns-wasm",
        "GOVERNANCE_CANISTER_TEST_WASM_PATH": "$(rootpath //rs/nns/governance:governance-canister-test)",
        "LEDGER_CANISTER_WASM_PATH": "$(rootpath //rs/ledger_suite/icp/ledger:ledger-canister-wasm)",
        "REGISTRY_CANISTER_WASM_PATH": "$(rootpath //rs/registry/canister:registry-canister)",
        "REGISTRY_CANISTER_TEST_WASM_PATH": "$(rootpath //rs/registry/canister:registry-canister-test)",
        "ROOT_CANISTER_WASM_PATH": "$(rootpath //rs/nns/handlers/root/impl:root-canister)",
        "SNS_WASM_CANISTER_WASM_PATH": "$(rootpath :sns-wasm-canister)",
        "MAINNET_SNS_WASM_CANISTER_WASM_PATH": "$(rootpath @mainnet_canisters//:sns-wasm.wasm.gz)",
        "NODE_REWARDS_CANISTER_WASM_PATH": "$(rootpath //rs/node_rewards/canister:node-rewards-canister)",
        "CYCLES_MINTING_CANISTER_WASM_PATH": "$(rootpath //rs/nns/cmc:cycles-minting-canister)",
        "GENESIS_TOKEN_CANISTER_WASM_PATH": "$(rootpath //rs/nns/gtc:genesis-token-canister)",
        "SNS_ROOT_CANISTER_WASM_PATH": "$(rootpath //rs/sns/root:sns-root-canister)",
        "SNS_GOVERNANCE_CANISTER_WASM_PATH": "$(rootpath //rs/sns/governance:sns-governance-canister)",
        "IC_ICRC1_INDEX_NG_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/index-ng:index_ng_canister)",
        "IC_ICRC1_LEDGER_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/ledger:ledger_canister)",
        "SNS_SWAP_CANISTER_WASM_PATH": "$(rootpath //rs/sns/swap:sns-swap-canister)",
        "IC_ICRC1_ARCHIVE_WASM_PATH": "$(rootpath //rs/ledger_suite/icrc1/archive:archive_canister)",
        "LIFELINE_CANISTER_WASM_PATH": "$(rootpath //rs/nns/handlers/lifeline/impl:lifeline_canister)",
        "UNSTOPPABLE_SNS_ROOT_CANISTER_WASM_PATH": "$(rootpath //rs/nns/integration_tests:unstoppable-sns-root-canister)",
        "UNIVERSAL_CANISTER_WASM_PATH": "$(rootpath //rs/universal_canister/impl:universal_canister.wasm.gz)",
    },
    extra_srcs = ["tests/common/mod.rs"],
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    deps = [":sns-wasm"] + DEPENDENCIES + DEV_DEPENDENCIES,
)

generated_files_check(
    name = "check_generated_files",
    srcs = ["tests/check_generated_files.rs"],
    data = glob(["src/gen/**"]) + [
        ":protos",
        "//rs/nervous_system/proto:protos",
        "//rs/sns/init:protos",
        "//rs/sns/swap:protos",
        "//rs/types/base_types:protos",
    ],
    manifest_dir = "rs/nns/sns-wasm",
    deps = [
        # Keep sorted.
        "//rs/nns/sns-wasm/protobuf_generator:lib",
        "//rs/test_utilities/compare_dirs",
        "@crate_index//:tempfile",
    ],
)
